package com.ruoyi.web.controller.asset;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import java.util.List;

import com.ruoyi.common.core.domain.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.asset.domain.FixedAssets;
import com.ruoyi.asset.service.IFixedAssetsService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;

/**
 * 固定资产信息Controller
 *
 * @author ruoyi
 * @date 2023-04-06
 */
@Controller
@RequestMapping("/asset/assets")
public class FixedAssetsController extends BaseController {
    private String prefix = "asset/assets";

    @Autowired
    private IFixedAssetsService fixedAssetsService;

    @RequiresPermissions("asset:assets:view")
    @GetMapping()
    public String assets() {
        return prefix + "/assets";
    }

    @RequiresPermissions("asset:assets:statement")
    @GetMapping("/statement")
    public String statemnet() {
        return prefix + "/statement";
    }

    /**
     * 查询固定资产信息列表
     */
    @RequiresPermissions("asset:assets:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FixedAssets fixedAssets) {
        startPage();
        List<FixedAssets> list = fixedAssetsService.selectFixedAssetsList(fixedAssets);
//        LambdaQueryWrapper<FixedAssets> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.like(StringUtils.isNotEmpty(fixedAssets.getAssetNumber()), FixedAssets::getAssetNumber, fixedAssets.getAssetNumber());
//        lambdaQueryWrapper.like(StringUtils.isNotEmpty(fixedAssets.getAssetName()), FixedAssets::getAssetName, fixedAssets.getAssetName());
//        lambdaQueryWrapper.like(StringUtils.isNotEmpty(fixedAssets.getAssetType()), FixedAssets::getAssetType, fixedAssets.getAssetType());
//        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(fixedAssets.getAssetBrand()), FixedAssets::getAssetBrand, fixedAssets.getAssetBrand());
//        lambdaQueryWrapper.like(StringUtils.isNotEmpty(fixedAssets.getAssetModel()), FixedAssets::getAssetModel, fixedAssets.getAssetModel());
//        List<FixedAssets> list = fixedAssetsService.list();
        return getDataTable(list);
    }

    /**
     * 导出固定资产信息列表
     */
    @RequiresPermissions("asset:assets:export")
    @Log(title = "固定资产信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FixedAssets fixedAssets) {
        List<FixedAssets> list = fixedAssetsService.selectFixedAssetsList(fixedAssets);
        ExcelUtil<FixedAssets> util = new ExcelUtil<FixedAssets>(FixedAssets.class);
        return util.exportExcel(list, "固定资产信息数据");
    }

    /**
     * 新增固定资产信息
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存固定资产信息
     */
    @RequiresPermissions("asset:assets:add")
    @Log(title = "固定资产信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FixedAssets fixedAssets) {
        return toAjax(fixedAssetsService.insertFixedAssets(fixedAssets));
    }

    /**
     * 修改固定资产信息
     */
    @RequiresPermissions("asset:assets:edit")
    @GetMapping("/edit/{assetNumber}")
    public String edit(@PathVariable("assetNumber") String assetNumber, ModelMap mmap) {
        FixedAssets fixedAssets = fixedAssetsService.selectFixedAssetsByassetNumber(assetNumber);
        mmap.put("fixedAssets", fixedAssets);
        return prefix + "/edit";
    }

    /**
     * 修改保存固定资产信息
     */
    @RequiresPermissions("asset:assets:edit")
    @Log(title = "固定资产信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FixedAssets fixedAssets) {
        return toAjax(fixedAssetsService.updateFixedAssets(fixedAssets));
    }

    /**
     * 删除固定资产信息
     */
    @RequiresPermissions("asset:assets:remove")
    @Log(title = "固定资产信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(fixedAssetsService.deleteFixedAssetsByassetNumbers(ids));
    }


    /**
     * 打印固定资产信息标签
     */
    @RequiresPermissions("asset:assets:print")
    @Log(title = "固定资产信息", businessType = BusinessType.OTHER)
    @GetMapping("/printById")
    @ResponseBody
    public void printById(@RequestParam("id") String id, HttpServletResponse response) throws IOException {

        File file = fixedAssetsService.printFixedAssetsInfo(id);
        FileInputStream fis = new FileInputStream(file);
        byte[] data = new byte[fis.available()];
        fis.read(data);
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "attachment; filename=example.pdf");
        response.getOutputStream().write(data);
        fis.close();
    }


    @RequiresPermissions("asset:assets:prints")
    @Log(title = "批量打印固定资产信息", businessType = BusinessType.OTHER)
    @GetMapping("/prints")
    @ResponseBody
    public void prints(@RequestParam("ids") String ids, HttpServletResponse response) throws IOException {
        File file = fixedAssetsService.printFixedAssetsInfoList(ids);
        FileInputStream fis = new FileInputStream(file);
        byte[] data = new byte[fis.available()];
        fis.read(data);
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "attachment; filename=example.pdf");
        response.getOutputStream().write(data);
        fis.close();
    }

    @RequiresPermissions("asset:assets:statementList")
    @Log(title = "固定资产报表", businessType = BusinessType.OTHER)
    @PostMapping("/statementList")
    @ResponseBody
    public R<List<FixedAssets>> getStatementList(FixedAssets fixedAssets) {
        List<FixedAssets> fixedAssetsList = fixedAssetsService.getStatementList(fixedAssets);
        return R.ok(fixedAssetsList);
    }
}
